import { defineStore } from 'pinia'


export interface Marker {
    featureType: string
    markType: string;
    id: string;
    pid: string;  //父标签ID
    ppageId: number;  //父标签所在的页ID
    pageId: number;
    selected?: boolean;
    shape: { x: number, y: number, width: number, height: number };
    childs: Marker[];
    props?:any,
    resourceTotal: number;
}

export interface Page {
    id: number;
    bookId:number;
    pageName:string;
    pageImg: string;
    markerList: Marker[];
    pageNum: string;
    pageType:string;
    edit:Boolean|false;
}

// export interface CataLog {
//     name: string;
//     pages: Page[];
// }

export interface Book {
    id: string;
    name: string;
    grade:number;
    edition:number;
    section: number;
    subject: number;
    pages: Page[];
}

export interface MarkerState {
    book: Book;
    currectPageIndex: number;
    currectPlusMarker: any;
    bookmenu:BookMenu[];
    freatureTypes:MarkerType[];
}

export interface MarkerType {
    mode: string;
    value: string;
    label: string;
}

export interface BookMenu {
    id: number;
    pid:number;
    text: string;
    bookId:string;
    pageIds: string;
    canDel:boolean;
    canEdit:boolean;
    canDrag:boolean;
    sort:number;
    children: BookMenu[];
}

export const useBookEditorStore = defineStore('book-editor-store', {
    state: (): MarkerState => ({
        book: {
            id: "",
            name: "",
            grade:0,
            section: 0,
            subject: 0,
            edition:0,
            pages: []
        },
        currectPageIndex: 0,
        currectPlusMarker: {},
        bookmenu:[],
        freatureTypes:[
            {
                mode:"PAN",
                value: "-1",
                label: "移动",
            },
            {
                mode:"RECT",
                value: "0",
                label: "标题",
            },
            {
                mode:"RECT",
                value: "1",
                label: "正文",
            },
            {
                mode:"RECT",
                value: "2",
                label: "题目",
            }
          ]
    }),
    getters: {
        currectPage(state): Page {
            return state.book.pages[state.currectPageIndex]
        },
        pages(state): Page[] {
            return state.book.pages;
        },
        markerList(state): Marker[] {
            return state.book.pages[state.currectPageIndex].markerList
        },
        imgUrl(state): string {
            return state.book.pages[state.currectPageIndex].pageImg
        },
        bookId(state): string {
            return state.book.id;
        },
        bookName(state): string {
            return state.book.name;
        },
        currectPageId(state):  number{
            return state.book.pages[state.currectPageIndex].id;
        },
        markTypes(state):  MarkerType[]{
            return state.freatureTypes;
        }

    },
    actions: {
        setBook(book: any) {
            this.book = book;
        },
        setPageId(pageId: number) {
            try {
                this.book.pages.forEach((page, pageIndex) => {
                    if (page.id == pageId) {
                        this.currectPageIndex = pageIndex;
                        throw new Error("退出循环");
                    }
                });
            } catch (e) { }

        }
    }
})